//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include "private.h"
#include <buola/dbus/cmessage.h>
#include <buola/utility/uvalue.h>
#include <buola/io/curi.h>
#include <buola/dbus/cproxy.h>

namespace buola { namespace dbus {

void append_value(CMessage *pM,char pV)
{
    pM->AppendBasic(DBUS_TYPE_BYTE,&pV);
}

void append_value(CMessage *pM,wchar_t pV)
{
    char lV=(char)pV;
    pM->AppendBasic(DBUS_TYPE_BYTE,&lV);
}

void append_value(CMessage *pM,int8_t pV)
{
    pM->AppendBasic(DBUS_TYPE_BYTE,&pV);
}

void append_value(CMessage *pM,uint8_t pV)
{
    pM->AppendBasic(DBUS_TYPE_BYTE,&pV);
}

void append_value(CMessage *pM,int16_t pV)
{
    pM->AppendBasic(DBUS_TYPE_INT16,&pV);
}

void append_value(CMessage *pM,uint16_t pV)
{
    pM->AppendBasic(DBUS_TYPE_UINT16,&pV);
}

void append_value(CMessage *pM,int32_t pV)
{
    pM->AppendBasic(DBUS_TYPE_INT32,&pV);
}

void append_value(CMessage *pM,uint32_t pV)
{
    pM->AppendBasic(DBUS_TYPE_UINT32,&pV);
}

void append_value(CMessage *pM,int64_t pV)
{
    pM->AppendBasic(DBUS_TYPE_INT64,&pV);
}

void append_value(CMessage *pM,uint64_t pV)
{
    pM->AppendBasic(DBUS_TYPE_UINT64,&pV);
}

void append_value(CMessage *pM,float pV)
{
    double lV=pV;
    pM->AppendBasic(DBUS_TYPE_DOUBLE,&lV);
}

void append_value(CMessage *pM,double pV)
{
    pM->AppendBasic(DBUS_TYPE_DOUBLE,&pV);
}

void append_value(CMessage *pM,bool pV)
{
    uint32_t lV=pV?1:0;
    pM->AppendBasic(DBUS_TYPE_UINT32,&lV);
}

void append_value(CMessage *pM,const char *pV)
{
    pM->AppendBasic(DBUS_TYPE_STRING,&pV);
}

void append_value(CMessage *pM,const std::string &pV)
{
    append_value(pM,pV.c_str());
}

void append_value(CMessage *pM,const std::wstring &pV)
{
    append_value(pM,utf8(pV).c_str());
}

void append_value(CMessage *pM,const io::CURI &pV)
{
    append_value(pM,pV.ToString());
}

void append_value(CMessage *pM,const UValue &pV)
{
    pM->StartStruct();
    append_value(pM,pV.Type());
    append_value(pM,pV.Value());
    pM->EndStruct();
}

void append_value(CMessage *pM,const CObjectPath &pV)
{
    const char *lV=pV.Get().c_str();
    
    pM->AppendBasic(DBUS_TYPE_OBJECT_PATH,&lV);
}

void extract_value(CMessage *pM,char &pV)
{
    pM->ExtractBasic(DBUS_TYPE_BYTE,&pV);
}

void extract_value(CMessage *pM,wchar_t &pV)
{
    char lV;
    pM->ExtractBasic(DBUS_TYPE_BYTE,&lV);
    pV=lV;
}

void extract_value(CMessage *pM,int8_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_BYTE,&pV);
}

void extract_value(CMessage *pM,uint8_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_BYTE,&pV);
}

void extract_value(CMessage *pM,int16_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_INT16,&pV);
}

void extract_value(CMessage *pM,uint16_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_UINT16,&pV);
}

void extract_value(CMessage *pM,int32_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_INT32,&pV);
}

void extract_value(CMessage *pM,uint32_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_UINT32,&pV);
}

void extract_value(CMessage *pM,int64_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_INT64,&pV);
}

void extract_value(CMessage *pM,uint64_t &pV)
{
    pM->ExtractBasic(DBUS_TYPE_UINT64,&pV);
}

void extract_value(CMessage *pM,float &pV)
{
    double lV;
    pM->ExtractBasic(DBUS_TYPE_DOUBLE,&lV);
    pV=(float)lV;
}

void extract_value(CMessage *pM,double &pV)
{
    pM->ExtractBasic(DBUS_TYPE_DOUBLE,&pV);
}

void extract_value(CMessage *pM,bool &pV)
{
    uint32_t lV;
    pM->ExtractBasic(DBUS_TYPE_BOOLEAN,&lV);
    pV=lV;
}

void extract_value(CMessage *pM,std::string &pV)
{
    const char *lV;
    pM->ExtractBasic(DBUS_TYPE_STRING,&lV);
    pV=lV;
}

void extract_value(CMessage *pM,std::wstring &pV)
{
    std::string lV;
    extract_value(pM,lV);
    pV=utf32(lV);
}

void extract_value(CMessage *pM,io::CURI &pV)
{
    std::string lV;
    extract_value(pM,lV);
    pV=io::uri(lV);
}

void extract_value(CMessage *pM,UValue &pV)
{
    std::string lType,lValue;
    pM->OpenContainer();
    extract_value(pM,lType);
    extract_value(pM,lValue);
    pM->CloseContainer();
    pV=UValue(lType,lValue);
}

void extract_value(CMessage *pM,CPath &pV)
{
    pV=CPath(pM->GetPath());
}

void extract_value(CMessage *pM,CMember &pV)
{
    pV=CMember(pM->GetMember());
}

void extract_value(CMessage *pM,CInterface &pV)
{
    pV=CInterface(pM->GetInterface());
}

void extract_value(CMessage *pM,CProxy &pV)
{
    const char *lV;
    pM->ExtractBasic(DBUS_TYPE_OBJECT_PATH,&lV);
    pV=CProxy(pM->GetBus(),pM->GetSender(),lV);
}

/*namespace dbus*/ } /*namespace buola*/ }
